package com.example.springboot.service;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.log.dialect.console.ConsoleLog;
import com.example.springboot.common.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.utils.TokenUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Console;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 功能：
 * 作者：sharon
 * 日期：2024-06-16 9:36
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public void insertUser(User user){
            userMapper.insert(user);
    }

    @Override
    public void updatetUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    @Override
    public void batchDeleteUser(List<Integer> ids) {
        for (Integer id : ids) {
            userMapper.deleteUser(id);
        }
    }

    @Override
    public List<User> selectAll() {
        return userMapper.selectAll();
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public List<User> selectByName(String name) {
        return userMapper.selectByName(name);
    }

    @Override
    public List<User> selectByMore(String username, String name) {
        return userMapper.selectByMore(username, name);
    }

    @Override
    public List<User> selectByMo(String username, String name) {
        return userMapper.selectByMo(username, name);
    }

    @Override
    public Page<User> selectByPage(Integer pageNum, Integer pageSize, String username, String name) {
        Integer skipNum =(pageNum - 1) * pageSize; //计算出来
        Page<User> page = new Page<>();

        List<User> userList = userMapper.selectByPage(skipNum, pageSize, username, name);
        Integer total = userMapper.selectCountByPage(username, name);
        page.setList(userList);
        page.setTotal(total);

        return page;
    }

    //验证用户账户是否合法
    @Override
    public User login(User user) {
        //根据用户名查询数据库的用户信息
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (dbUser == null) {
            //抛出一个自定义的异常
            throw new ServiceException("用户名或密码错误");
        }else if (!user.getPassword().equals(dbUser.getPassword())){
            throw new ServiceException("用户名或密码错误");
        }else if (dbUser.getId() == null){
            throw new ServiceException("数据有问题....");
        }
        // 生成token
        String token = TokenUtils.createToken(dbUser.getId().toString(), dbUser.getPassword());
        dbUser.setToken(token);
        return dbUser;
    }

    @Override
    public User register(User user) {
        User dbUser = userMapper.selectByUsername(user.getUsername());
        if (dbUser != null) {
            //抛出一个自定义的异常
            throw new ServiceException("用户名已存在");
        }
        user.setName("honey-" + RandomUtil.randomNumbers(4));
        userMapper.insert(user);
        return user;
    }

}
